//
//  HamatorWheelSensorViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/4/22.
// 车辆轮位传感器设置

import UIKit

class HamatonWheelSensorViewController: HamatonBaseViewController {
    var tyreSettingModel: TyreSettingModel = TyreSettingModel()
    weak var delegate: HamatonWheelSensorDelegate?
    var canEditThreshold: Bool = false
    var isBind: Bool = false // 轮胎是否绑定
    
    // 传感器类型
    var ChuanganqiTypeBaudRateModel: HamatonCarModel? {
        didSet {
            chuanganqiRightLabel.text = ChuanganqiTypeBaudRateModel?.name ?? ""
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .clear
        navigationItem.title = "车辆轮位传感器设置"
        let margTop: Float = 15
        let margRight: Float = -20
        let margLeft: Float = 20
        // 轮胎位置
        view.addSubview(luntaiweizhiLabel)
        view.addSubview(luntaiweizhiRightLabel)
        view.addSubview(lineView1)
        // 传感器类型
        view.addSubview(chuanganqileixingLabel)
        view.addSubview(chuanganqiRightLabel)
        view.addSubview(chuanganqiRightImg)
        view.addSubview(chuanganqiRightButton)
        view.addSubview(lineView2)
        // 传感器ID
        view.addSubview(chuanganqiIDLabel)
//        view.addSubview(chuanganqiIDDanweiLabel)
        view.addSubview(chuanganqiIDTextField)
        view.addSubview(lineView3)
        
        // 低压
        view.addSubview(diyaLabel)
        view.addSubview(diyaDanweiLabel)
        view.addSubview(diyaTextField)
        view.addSubview(lineView4)
        // 最低压
        view.addSubview(zuidiyaLabel)
        view.addSubview(zuidiyaDanweiLabel)
        view.addSubview(zuidiyaTextField)
        view.addSubview(lineView5)
        // 高压
        view.addSubview(gaoyaLabel)
        view.addSubview(gaoyaDanweiLabel)
        view.addSubview(gaoyaTextField)
        view.addSubview(lineView6)
        // 高温
        view.addSubview(gaowenLabel)
        view.addSubview(gaowenDanweiLabel)
        view.addSubview(gaowenTextField)
        view.addSubview(lineView7)
        // 传感器丢失报警时间
        view.addSubview(tpmsLabel)
        view.addSubview(tpmsDanweiLabel)
        view.addSubview(tpmsTextField)
        view.addSubview(lineView8)
        // 解除绑定
        view.addSubview(cancelBtn)
        // 保存
        view.addSubview(saveBtn)
        
        // 轮胎位置
        luntaiweizhiLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        luntaiweizhiRightLabel.snp.makeConstraints { make in
            make.centerY.equalTo(luntaiweizhiLabel.snp.centerY)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(60)
            make.height.equalTo(20)
        }
        
        lineView1.snp.makeConstraints { make in
            make.top.equalTo(luntaiweizhiLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 传感器类型
        chuanganqileixingLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        chuanganqiRightImg.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop+4)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
//        chuanganqiRightButton.snp.makeConstraints { make in
//            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
//            make.right.equalToSuperview().offset(margRight)
//            make.width.equalTo(40)
//            make.height.equalTo(20)
//        }
        chuanganqiRightLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.right.equalTo(chuanganqiRightImg.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView2.snp.makeConstraints { make in
            make.top.equalTo(chuanganqileixingLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        chuanganqiRightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView2.snp.top).offset(0)
        }
        // 传感器ID
        chuanganqiIDLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
//        chuanganqiIDDanweiLabel.snp.makeConstraints { make in
//            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
//            make.right.equalToSuperview().offset(margRight)
//            make.width.equalTo(40)
//            make.height.equalTo(20)
//        }
        chuanganqiIDTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView3.snp.makeConstraints { make in
            make.top.equalTo(chuanganqiIDLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 低压
        diyaLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        diyaDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        diyaTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView3.snp.bottom).offset(margTop)
            make.right.equalTo(diyaDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView4.snp.makeConstraints { make in
            make.top.equalTo(diyaLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 最低压
        zuidiyaLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        zuidiyaDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        zuidiyaTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView4.snp.bottom).offset(margTop)
            make.right.equalTo(zuidiyaDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView5.snp.makeConstraints { make in
            make.top.equalTo(zuidiyaLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 高压
        gaoyaLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        gaoyaDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        gaoyaTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView5.snp.bottom).offset(margTop)
            make.right.equalTo(gaoyaDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView6.snp.makeConstraints { make in
            make.top.equalTo(gaoyaLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 高温
        gaowenLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView6.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(120)
            make.height.equalTo(20)
        }
        gaowenDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView6.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        gaowenTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView6.snp.bottom).offset(margTop)
            make.right.equalTo(gaowenDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView7.snp.makeConstraints { make in
            make.top.equalTo(gaowenLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        // 传感器丢失报警时间
        tpmsLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView7.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(150)
            make.height.equalTo(20)
        }
        tpmsDanweiLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView7.snp.bottom).offset(margTop)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(40)
            make.height.equalTo(20)
        }
        tpmsTextField.snp.makeConstraints { make in
            make.top.equalTo(lineView7.snp.bottom).offset(margTop)
            make.right.equalTo(tpmsDanweiLabel.snp.left).offset(margRight)
            make.width.equalTo(200)
            make.height.equalTo(20)
        }
        
        lineView8.snp.makeConstraints { make in
            make.top.equalTo(tpmsLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        // 解除绑定
        cancelBtn.snp.makeConstraints { make in
            make.top.equalTo(lineView8.snp.bottom).offset(margTop*3)
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
        }
        
        // 保存
        saveBtn.snp.makeConstraints { make in
            make.top.equalTo(cancelBtn.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(26)
            make.right.equalToSuperview().offset(-26)
            make.height.equalTo(40)
        }
    }

    // 轮胎位置
    private lazy var luntaiweizhiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "轮胎位置"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var luntaiweizhiRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x999999)
        label.font = Theme.Font.size(16)
        label.text = tyreSettingModel.luntaiweizhiStr
        label.backgroundColor = .clear
        label.textAlignment = .right
        return label
    }()
    
//    lazy var luntaiweizhiTextField: UIView = {
//        let textField = UITextField()
//        textField.placeholder = "请输入"
//        textField.font = Theme.Font.size(16)
//        textField.text = ""
//        //右侧删除
//        textField.clearButtonMode = .whileEditing
//        textField.tintColor = UIColor(hex6: 0x222222)
//      //  textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
//        textField.textAlignment = .right
//        textField.contentVerticalAlignment = .center
//        return textField
//    }()
    
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 传感器类型
    private lazy var chuanganqileixingLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "传感器类型"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
//    lazy var chuanganqileixingTextField: UIView = {
//        let textField = UITextField()
//        textField.placeholder = "请输入"
//        textField.font = Theme.Font.size(16)
//        textField.text = ""
//        //右侧删除
//        textField.clearButtonMode = .whileEditing
//        textField.tintColor = UIColor(hex6: 0x222222)
//      //  textField.addTarget(self, action: #selector(macAddressTextFieldDidChange(tf:)), for: .editingChanged)
//        textField.textAlignment = .right
//        textField.contentVerticalAlignment = .center
//        return textField
//    }()
//    
//    
//    lazy var chuanganqileixingDanweiLabel: UILabel = {
//        let label = UILabel()
//        label.textColor = UIColor(hex6:0x404040)
//        label.font = Theme.Font.size(16)
//        label.text = "kpa"
//        label.backgroundColor = .clear
//        label.textAlignment = .left
//        return label
//    }()
    lazy var chuanganqiRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        return view
    }()
    lazy var chuanganqiRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onSelectCgqType), for: .touchUpInside)
        return button
    }()
    lazy var chuanganqiRightLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = CgqTypeOption1[tyreSettingModel.chuanganqiTypeId]
        label.backgroundColor = .clear
        label.textAlignment = .right
        return label
    }()
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 传感器ID
    private lazy var chuanganqiIDLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "传感器ID"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var chuanganqiIDTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(16)
        textField.text = tyreSettingModel.chuanganqiIDStr
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
//    lazy var chuanganqiIDDanweiLabel: UILabel = {
//        let label = UILabel()
//        label.textColor = UIColor(hex6:0x404040)
//        label.font = Theme.Font.size(16)
//        label.text = "kpa"
//        label.backgroundColor = .clear
//        label.textAlignment = .left
//        return label
//    }()
    
    private lazy var lineView3: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 低压
    private lazy var diyaLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "低压"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var diyaTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.font = Theme.Font.size(16)
        textField.textColor = canEditThreshold ? UIColor(hex6: 0x404040) :  UIColor(hex6: 0x999999)
        textField.text = tyreSettingModel.diyaStr
        // 设置为不可编辑
        textField.isEnabled = canEditThreshold
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
       textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var diyaDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "kpa"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView4: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 最低压
    private lazy var zuidiyaLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "最低压"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var zuidiyaTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.textColor = canEditThreshold ? UIColor(hex6: 0x404040) :  UIColor(hex6: 0x999999)
        textField.font = Theme.Font.size(16)
        textField.text = tyreSettingModel.zuidiyaStr
        // 设置为不可编辑
        textField.isEnabled = canEditThreshold
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var zuidiyaDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "kpa"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView5: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 高压
    private lazy var gaoyaLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "高压"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var gaoyaTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.textColor = canEditThreshold ? UIColor(hex6: 0x404040) :  UIColor(hex6: 0x999999)
        textField.font = Theme.Font.size(16)
        textField.text = tyreSettingModel.gaoyaStr
        // 设置为不可编辑
        textField.isEnabled = canEditThreshold
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var gaoyaDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "kpa"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView6: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 高温
    private lazy var gaowenLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "高温"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var gaowenTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.textColor = canEditThreshold ? UIColor(hex6: 0x404040) :  UIColor(hex6: 0x999999)
        textField.font = Theme.Font.size(16)
        textField.text = tyreSettingModel.gaowenStr
        // 设置为不可编辑
        textField.isEnabled = canEditThreshold
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var gaowenDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "℃"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView7: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 传感器丢失报警时间
    private lazy var tpmsLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "传感器丢失报警时间"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    lazy var tpmsTextField: UIView = {
        let textField = UITextField()
        textField.placeholder = "请输入"
        textField.textColor = canEditThreshold ? UIColor(hex6: 0x404040) :  UIColor(hex6: 0x999999)
        textField.font = Theme.Font.size(16)
        textField.text = tyreSettingModel.shijianStr
        // 设置为不可编辑
        textField.isEnabled = canEditThreshold
        //右侧删除
        textField.clearButtonMode = .whileEditing
        textField.tintColor = UIColor(hex6: 0x222222)
        textField.addTarget(self, action: #selector(textFieldDidChange(tf:)), for: .editingChanged)
        textField.textAlignment = .right
        textField.contentVerticalAlignment = .center
        return textField
    }()
    
    lazy var tpmsDanweiLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "min"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    
    private lazy var lineView8: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    
    // 解除绑定
    private lazy var cancelBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("解除绑定", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(16, bold: true)
        button.addTarget(self, action: #selector(onClickCancelButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.maintwo
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        return button
    }()
    
    // 保存设置
    private lazy var saveBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle(NSLocalizedString("保存设置", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(16, bold: true)
        button.addTarget(self, action: #selector(onClickSaveButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.main
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5
        return button
    }()
    
    // 输入框内容改变
    @objc func textFieldDidChange(tf: UITextField) {
        if tf == chuanganqiIDTextField {
            var tftext = tf.text ?? ""
            tftext = String(tftext.prefix(8))
            tyreSettingModel.chuanganqiIDStr = tftext
        } else if tf == diyaTextField {
            var tftext = tf.text ?? ""
            tyreSettingModel.diyaStr = tftext
        } else if tf == zuidiyaTextField {
            var tftext = tf.text ?? ""
            tyreSettingModel.zuidiyaStr = tftext
        } else if tf == gaoyaTextField {
            var tftext = tf.text ?? ""
            tyreSettingModel.gaoyaStr = tftext
        } else if tf == gaowenTextField {
            var tftext = tf.text ?? ""
            tyreSettingModel.gaowenStr = tftext
        } else if tf == tpmsTextField {
            var tftext = tf.text ?? ""
            tyreSettingModel.shijianStr = tftext
        }
    }

    // 选择传感器类型
    @objc func onSelectCgqType() {
        let vc: HamatonCarSettingSelectViewController = HamatonCarSettingSelectViewController()
        vc.selectTypeEnumFun(selectTypeEnum: .h)
        weak var weakSelf = self
        vc.backAction = {  (model: HamatonCarModel) -> Void in
            weakSelf?.ChuanganqiTypeBaudRateModel = model
            print("获取传感器类型了")
            weakSelf?.tyreSettingModel.chuanganqiTypeId = model.id
        }
        self.navigationController?.pushViewController(vc, animated: false)
    }
    // 解除绑定
    @objc func onClickCancelButton() {
        if isBind {
            delegate?.cancelWheelSensor(tyreSettingModel)
        } else {
            self.view.makeToast("该轮胎暂未绑定传感器", duration: 3.0, position: .center)
        }
        
    }
    // 保存轮胎传感器
    @objc func onClickSaveButton() {
        if tyreSettingModel.chuanganqiTypeId == "" {
            self.view.makeToast("请选择传感器类型", duration: 3.0, position: .center)
        } else if tyreSettingModel.chuanganqiIDStr == "" {
            self.view.makeToast("请输入传感器ID", duration: 3.0, position: .center)
        } else {
            delegate?.saveWheelSensor(tyreSettingModel)
        }
    }
}
